// pages/avatar/avatarChange.js
import {
    baseUrl,
    // isHaveToken
} from "../../utils/util";
Page({

    /**
     * 页面的初始数据
     */
    data: {
        originFiles: [
            {
                url: 'https://tdesign.gtimg.com/site/upload1.png',
                name: 'uploaded1.png',
                type: 'image',
            },
        ],
        gridConfig: {
            column: 1,
            width: wx.getSystemInfoSync().windowWidth,
            height: wx.getSystemInfoSync().windowWidth,
        },
        config: {
            count: 1,
        },
        disabled: true,
        loading: false,
        imageUploaded: "",
        myInfo:{}
    },
    handleSuccess(e) {
        console.log(e)
        const { files } = e.detail;
        this.setData({
            originFiles: files,
        });
        this.uploadImage()
    },
    handleRemove(e) {
        console.log(e)
        const { index } = e.detail;
        const { originFiles } = this.data;
        originFiles.splice(index, 1);
        this.setData({
            originFiles,
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        // console.log("options",options)
        console.log("options JSON.parse", JSON.parse(options.avatar))
        var myInfo = JSON.parse(options.avatar)
        console.log("myInfo avatar", myInfo.avatar)
        this.setData({
            originFiles: [
                {
                    url: myInfo.avatar,
                    name: 'myAvatar.png',
                    type: 'image',
                },
            ],
            myInfo:myInfo
        })
    },
    //上传图片
    uploadImage() {
        var image = []
        var that = this
        that.setData({
            loading: true,
            disabled: true
        })
        console.log("originFilex", this.data.originFiles[0].url)
        wx.uploadFile({
            filePath: this.data.originFiles[0].url,
            name: 'file',
            url: `${baseUrl}/oss/label`,
            success(res) {
                var data = JSON.parse(res.data)
                console.log(data)
                console.log("data.data", data.data)
                // console.log("data.image", image)
                console.log("image ", image.length)
                if (data.code == 20000) {
                    console.log("我上传完啦")
                    that.setData({
                        finish: true,
                        loading: false,
                        disabled: false,
                        imageUploaded:data.data.url
                    })
                }
            }
        })

        // wx.request({
        //   url: `${baseUrl}/oss/label`,
        //   files:this.data.originFiles[0],
        //   success(res){
        //       console.log(res)
        //   }
        // })
    },
    // 上传按钮
    upload(e) {
        console.log("myInfo",this.data.myInfo)
        // console.log(this.data.originFiles)
        // console.log("originFiles lenght", this.data.originFiles.length)
        // this.uploadFile()
        // console.log("imageUploaded",this.data.imageUploaded)
        this.changeAvatar()
        this.setData({
            // originFiles: [],
            loading: false
        })

    },
    getUserInfo() {
        var that = this
        wx.request({
            header: {
                token: wx.getStorageSync('token')
            },
            url: `${baseUrl}/ucenter/member/getMemberInfo`,
            success(res) {
                console.log("userInfo", res)
                that.setData({
                        userInfo: res.data.data.userInfo
                    }),
                    wx.setStorageSync('userInfo', res.data.data.userInfo)
            }
        })
    },
    changeAvatar() {
        console.log("imageUploaded",this.data.imageUploaded)
        console.log("myInfo id",this.data.myInfo.id)
        var that = this
        wx.request({
            url: `${baseUrl}/ucenter/member/changeInfo`,
            method: "POST",
            data: {
                id: that.data.myInfo.id,
                // id: "1577196969086947329",
                avatar: that.data.imageUploaded
                // avatar:"string"
            },
            success(res) {
                console.log("GetSingChange", res)
                // that.setData({
                //     // isChage:true,
                //     sign: ""
                // })
                that.getUserInfo()
                if(res.data.code==20000){
                    wx.navigateBack({
                      delta: 0,
                      success: (res) => {},
                      fail: (res) => {},
                      complete: (res) => {},
                    })
                }
                
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})